package com.gray.blog.controller;

import com.gray.blog.common.enums.ErrorCodeEnum;
import com.gray.blog.common.result.ModelResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Created  By zhoux 2019/01/11
 **/
@RestControllerAdvice
@Slf4j
public class ExceptionController {

    /**
     * 鉴权错误处理
     * @return
     */
    @ExceptionHandler(AccountException.class)
    public ModelResult handleShiroException() {
        return ModelResult.newError(ErrorCodeEnum.TOKEN_INVALID);
    }

    /**
     * 鉴权错误处理
     * @return
     */
    @ExceptionHandler(UnauthenticatedException.class)
    public ModelResult UnauthenticatedException() {
        return ModelResult.newError(ErrorCodeEnum.USER_NO_PERMISSION);
    }

    /**
     * 身份错误处理
     * @return
     */
    @ExceptionHandler(UnauthorizedException.class)
    public ModelResult handleNoRoleException() {
        return ModelResult.newError(ErrorCodeEnum.USER_NO_PERMISSION);
    }

    /**
     * 参数错误处理
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ModelResult MethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error("MethodArgumentNotValidException e:{}", e);
        return ModelResult.newError(ErrorCodeEnum.PARAMS_ERROR);
    }

    /**
     * 统一错误处理
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ModelResult globalException(Exception e) {
        log.warn("globalException e:{}", e);
        return ModelResult.newError(ErrorCodeEnum.SYSTEM_ERROR);
    }




}
